﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class UIWF : ViewBase
    {
        
        public UIWF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MESSAGES += "CGRPCHG;";
         } 
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWF = dwMaster;
            if (dwMaster == null)
            {
                KEY = "";
                CGRP = ""; 
            }
            else
            { 
                KEY = TMV.WAFART + TMV.WAFVAR + TMV.WAFZUART + TMV.WAFZUSATZ;
                CGRP = TMV.WAFPRGRP;
            }
        }
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {               
                case "VFCHG":
                    VFCHG(msg);
                    break; 
            } 
        }   
       

        public void VFCHG(MessageInfo msg)
        {
            if (dwWF == null)
                return;
            dwWF["WAFBEZ"] = msg.P1; 
            ((VWAFARBEN)MV).UpdateWAFARBEN(dwWF, app.company, 0);
        } 

        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                base.SaveCommand(sender, e);
                TMV.UpdateWAFARBEN(dwMaster,app.company,0);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged) continue;
                TMV.UpdateWAFARBEN(row, app.company, 0);
            } 
            base.OnSave();
        } 
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            TBMain.Clear();
            if (ORDERNUM.Length < 32)
                return;            
            dwData = TBMain.NewRow();
            dwData["WAFMANDANT"] = app.Mandant;
            dwData["WAFART"] = ORDERNUM.Substring(1,20);
            dwData["WAFVAR"] = ORDERNUM.Substring(21, 2);//从VF过来，因为纱线是3个VAR长度
            dwData["WAFZUSATZ"] = ORDERNUM.Substring(24, 10); 
            SResult = TMV.QWAF01CF(dwData);
            MergeData(SResult, true); 
        }

        public virtual void OnCARTChg(string value)
        { }

    }
    public partial class UIWF : ViewBase
    {
        public VWAFARBEN TMV
        {
            get
            {
                return (VWAFARBEN)MV;
            }
        }
       
        #region ==IsYD===========
        public static readonly DependencyProperty IsYDProperty = DependencyProperty.Register("IsYD",
        typeof(bool), typeof(UIWF),
        new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsYDChanged));
        public bool IsYD
        {
            get
            {
                return (bool)GetValue(IsYDProperty);
            }
            set
            {
                SetValue(IsYDProperty, value);
            }
        }
        private static void OnIsYDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.IsYD = (bool)e.NewValue;
                src.OnIsYDChg((bool)e.NewValue);
            }
        }
        public virtual void OnIsYDChg(bool value)
        {
             
        }
        #endregion
        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
                OnCARTChg(value);
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.CART = (string)e.NewValue;

            }
        }
        #endregion

        #region ==dwWF===========
        public static readonly DependencyProperty dwWFProperty = DependencyProperty.Register("dwWF",
            typeof(DataRow), typeof(UIWF),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWFChanged));
        public DataRow dwWF
        {
            get
            {
                return (DataRow)GetValue(dwWFProperty);
            }
            set
            {
                SetValue(dwWFProperty, value);
            }
        }
        private static void OndwWFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.dwWF = (DataRow)e.NewValue;
            }
        }
        #endregion

        #region ==dwWS===========
        public static readonly DependencyProperty dwWSProperty = DependencyProperty.Register("dwWS",
            typeof(DataRow), typeof(UIWF),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWSChanged));
        public DataRow dwWS
        {
            get
            {
                return (DataRow)GetValue(dwWSProperty);
            }
            set
            {
                SetValue(dwWSProperty, value);
                OnWSChg(value);
            }
        }
        private static void OndwWSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.dwWS = (DataRow)e.NewValue;
            }
        }
        public virtual void OnWSChg(DataRow ws)
        {

        }
        #endregion

        #region ==CGRP===========
        public static readonly DependencyProperty CGRPProperty = DependencyProperty.Register("CGRP",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCGRPChanged));
        public string CGRP
        {
            get
            {
                return (string)GetValue(CGRPProperty);
            }
            set
            {
                SetValue(CGRPProperty, value);
                if (!IsEdit)
                    return;
                if (dwMaster == null)
                    return;
                TMV.WAFPRGRP = CGRP; 
            }
        }
        private static void OnCGRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.CGRP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ZUART===========
        public static readonly DependencyProperty ZUARTProperty = DependencyProperty.Register("ZUART",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("C", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnZUARTChanged));
        public string ZUART
        {
            get
            {
                return (string)GetValue(ZUARTProperty);
            }
            set
            {
                SetValue(ZUARTProperty, value);
                OnZUARTChg();
            }
        }
        private static void OnZUARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.ZUART = (string)e.NewValue;
            }
        }
        public virtual void OnZUARTChg()
        {
            
        }
        #endregion

        #region ==WAFFDVF===========
        public static readonly DependencyProperty WAFFDVFProperty = DependencyProperty.Register("WAFFDVF",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFFDVFChanged));
        public string WAFFDVF
        {
            get
            {
                return (string)GetValue(WAFFDVFProperty);
            }
            set
            {
                SetValue(WAFFDVFProperty, value);
                OnWAFFDVFChg();
            }
        }
        private static void OnWAFFDVFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFFDVF = (string)e.NewValue;
            }
        }
        public virtual void OnWAFFDVFChg()
        {

            if (!IsEdit)
                return;
            TMV.WAFFDVF = WAFFDVF;
        }
        #endregion

        #region ==WAFPRGRP===========
        public static readonly DependencyProperty WAFPRGRPProperty = DependencyProperty.Register("WAFPRGRP",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFPRGRPChanged));
        public string WAFPRGRP
        {
            get
            {
                return (string)GetValue(WAFPRGRPProperty);
            }
            set
            {
                SetValue(WAFPRGRPProperty, value);
                OnWAFPRGRPChg();
            }
        }
        private static void OnWAFPRGRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFPRGRP = (string)e.NewValue;
            }
        }
        public virtual void OnWAFPRGRPChg()
        {

            if (!IsEdit)
                return;
            TMV.WAFPRGRP = WAFPRGRP;
        }
        #endregion 

        #region ==WAFNACHB===========
        public static readonly DependencyProperty WAFNACHBProperty = DependencyProperty.Register("WAFNACHB",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFNACHBChanged));
        public string WAFNACHB
        {
            get
            {
                return (string)GetValue(WAFNACHBProperty);
            }
            set
            {
                SetValue(WAFNACHBProperty, value);
                OnWAFNACHBChg();
            }
        }
        private static void OnWAFNACHBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFNACHB = (string)e.NewValue;
            }
        }
        public virtual void OnWAFNACHBChg()
        {

            if (!IsEdit)
                return;
            TMV.WAFNACHB = WAFNACHB;
        }
        #endregion
        #region ==WAFVORB===========
        public static readonly DependencyProperty WAFVORBProperty = DependencyProperty.Register("WAFVORB",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFVORBChanged));
        public string WAFVORB
        {
            get
            {
                return (string)GetValue(WAFVORBProperty);
            }
            set
            {
                SetValue(WAFVORBProperty, value);
                OnWAFVORBChg();
            }
        }
        private static void OnWAFVORBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFVORB = (string)e.NewValue;
            }
        }
        public virtual void OnWAFVORBChg()
        {

            if (!IsEdit)
                return;
            TMV.WAFVORB = WAFVORB;
        }
        #endregion

        #region ==WAFRUESTGRP===========
        public static readonly DependencyProperty WAFRUESTGRPProperty = DependencyProperty.Register("WAFRUESTGRP",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFRUESTGRPChanged));
        public string WAFRUESTGRP
        {
            get
            {
                return (string)GetValue(WAFRUESTGRPProperty);
            }
            set
            {
                SetValue(WAFRUESTGRPProperty, value);
                OnWAFRUESTGRPChg();
            }
        }
        private static void OnWAFRUESTGRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFRUESTGRP = (string)e.NewValue;
            }
        }
        public virtual void OnWAFRUESTGRPChg()
        {
            if (!IsEdit)
                return;
            TMV.WAFRUESTGRP = WAFRUESTGRP;

        }
        #endregion  

        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
        typeof(string), typeof(UIWF),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
                OnLIEFNRChg(value);
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
            }
        }
        public virtual void OnLIEFNRChg(string ls)
        { }
        #endregion

        #region ==IsStock===========
        public static readonly DependencyProperty IsStockProperty = DependencyProperty.Register("IsStock",
            typeof(bool), typeof(UIWF),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsStockChanged));
        public bool IsStock
        {
            get
            {
                return (bool)GetValue(IsStockProperty);
            }
            set
            {
                SetValue(IsStockProperty, value);
            }
        }
        private static void OnIsStockChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.IsStock = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==WAFBEZ===========
        public static readonly DependencyProperty WAFBEZProperty = DependencyProperty.Register("WAFBEZ",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFBEZChanged));
        public string WAFBEZ
        {
            get
            {
                return (string)GetValue(WAFBEZProperty);
            }
            set
            {
                SetValue(WAFBEZProperty, value);
            }
        }
        private static void OnWAFBEZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFBEZ = (string)e.NewValue;
                if (src.dwMaster == null)
                    return;
                src.TMV.dwMaster["WAFBEZ"] = (string)e.NewValue;
            }
        }
        #endregion
        #region ==WAFGRPBEZ===========
        public static readonly DependencyProperty WAFGRPBEZProperty = DependencyProperty.Register("WAFGRPBEZ",
            typeof(string), typeof(UIWF),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAFGRPBEZChanged));
        public string WAFGRPBEZ
        {
            get
            {
                return (string)GetValue(WAFGRPBEZProperty);
            }
            set
            {
                SetValue(WAFGRPBEZProperty, value);
            }
        }
        private static void OnWAFGRPBEZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWF;
            if (src != null)
            {
                src.WAFGRPBEZ = (string)e.NewValue;
                if (src.dwMaster == null)
                    return;
                src.TMV.dwMaster["WAFGRPBEZ"] = (string)e.NewValue;
            }
        }
        #endregion


    }
}

